<template>
  <views class="user_managen">
    <viewTitle :title="'用户管理'" :border="false"></viewTitle>
    <!-- 搜索栏  用户名 - 真实姓名 -->
    <div class="search flex flexC">
      <!-- 所属服务商-银行流水号-客户编号-记录类型 -->
      <el-form ref="form" label-width="auto" :inline="true">
        <div class="search_input flex flexR center">
          <div class="customer_number flex flexR center">
            <el-form-item label="真实姓名:">
              <el-input v-model="form.user_real_name" placeholder="请输入真实姓名" :clearable="true"></el-input>
            </el-form-item>
          </div>
          <div class="their_facilitator flex flexR center">
            <el-form-item label="用户名:">
              <el-input v-model="form.user_name" placeholder="请输入用户名" :clearable="true"></el-input>
            </el-form-item>
          </div>

          <!-- 按钮（查询、重置、新增）   -->
          <el-form-item>
            <div class="flex flexR pay_btn_list">
              <el-button type="primary" @click="getData" icon="el-icon-search">查询</el-button>
              <el-button @click="reSet" icon="el-icon-refresh">重置</el-button>
              <el-button type="danger" v-if="isAdd" @click="add_user">+新增</el-button>
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <!-- 展示列表 -->
    <div class="table_list">
      <el-table
        border
        :data="tableData"
        style="width: 100%;max-width:none;"
        :header-cell-style="getRowClass"
        :select-on-indeterminate="true"
        :fit="true"
        :lazy="true"
        @row-click="clickRow"
        max-height="450"
      >
        <el-table-column type="index" label="序号" width="100"></el-table-column>
        <el-table-column prop="user_name" label="用户名称" width="200"></el-table-column>
        <el-table-column prop="user_real_name" label="真实姓名" width="120"></el-table-column>
        <el-table-column
          prop="is_admin"
          :formatter="(row)=>{return row.is_admin ? '管理员':'非管理员'}"
          label="用户类型"
          width="120"
        ></el-table-column>
        <el-table-column
          prop="status"
          :formatter="(row)=>{return row.status == 0 ? '正常': (row.status == 1?'锁定':'无效')}"
          label="状态"
          width="300"
        ></el-table-column>
        <el-table-column
          prop="created_time"
          :formatter="(row)=>{ return  row.created_time > 0  ?  this.$moment(row.created_time*1000).format('YYYY-MM-DD HH:MM:SS'):''; }"
          label="创建时间"
          width="auto"
        ></el-table-column>
        <el-table-column label="操作" flex="right" width="200">
          <template slot-scope="scope">
            <div class="table_operation flex flexR">
              <el-button type="danger" v-if="isEdit" @click="amend_user(scope)">修改</el-button>
              <el-button type="primary" @click="look_user(scope)" v-if="isLook">查看</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="flex flexR paging">
      <el-pagination
        style="background:#fff; padding:10px 0"
        background
        layout="total, sizes,prev, pager, next, jumper"
        @size-change="(page_size) => {this.pagination.page_size = page_size;this.getData()}"
        @current-change="(page_size) => {this.pagination.page_start = page_size;this.getData()}"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="pagination.page_size"
        :total="pagination.total_count"
        :current-page="pagination.current_page"
        :page-count="pagination.page_count"
      ></el-pagination>
    </div>
  </views>
</template>



<script>
import { mapGetters } from 'vuex';

export default {
  data() {
    return {
      //  三段综合  企业端  代理商端  服务商端
      form: {
        user_name: "", //  用户名
        user_real_name: "" //  真实姓名
      },
      pagination: {
        page_start: 0, //搜索开始页码
        current_page: 0, //当前页码
        page_count: 0, //总页数
        page_size: 10, //每页数量
        total_count: 0 //总数量
      },
      tableData: [],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      }
    };
  },
  computed: {
    getTerrace() {
      return this.$store.state.terrace;
    },
    getSaveFacilitatorUser() {
      //查看该服务商管理员是否有新增/编辑 角色的权限
      return this.$store.state.buttonPermission.saveFacilitatorUser
        ? this.$store.state.buttonPermission.saveFacilitatorUser
        : "";
    },
    ...mapGetters(['getButtonPermission']),
    isAdd(){
      return  this.getButtonPermission[this.$route.path]  &&  this.getButtonPermission[this.$route.path].add ? 1 : 0;
    },
    isLook(){
      return  this.getButtonPermission[this.$route.path]  &&  this.getButtonPermission[this.$route.path].look ? 1 : 0;
    },
    isEdit(){
      return  this.getButtonPermission[this.$route.path]  &&  this.getButtonPermission[this.$route.path].edit ? 1 : 0;
    },
    isDelete(){
      return  this.getButtonPermission[this.$route.path]  &&  this.getButtonPermission[this.$route.path].delete ? 1 : 0;
    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    //    修改第一行样式
    getRowClass({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return "background: #FAFAFA ";
      } else {
        return "";
      }
    },
    //     点击单行
    clickRow(row, column, event) {
      console.log(row, column, event);
    },
    //    修改是否换行
    changeStyleRow(width) {
      let _width = width - 290 - 350 - 300 - 50 - 25;
      if (_width > 300) {
        this.marginLeft = 25;
      } else {
        this.marginLeft = 0;
      }
    },
    // 新增用户
    add_user() {
      this.$toRoutePath({
        path: "/user_managen/user_managen_add",
        edit: true,
        query: {}
      });
    },
    // 修改用户
    amend_user(scope) {
      this.$toRoutePath({
        path: "/user_managen/user_managen_amend",
        edit: true,
        query: { user_id: scope.row.user_id }
      });
    },
    look_user(scope) {
      this.$toRoutePath({
        path: "/user_managen/user_managen_look",
        edit: true,
        query: { user_id: scope.row.user_id }
      });
    },
    getData() {
      console.log("ssss", this.$store.state.terrace);
      this.form.pagination = this.pagination;
      if (this.$store.state.terrace == 1) {
        this.getEnterpriseData();
      }
      if (this.$store.state.terrace == 2) {
        this.getAgentData();
      }
      if (this.$store.state.terrace == 3) {
        this.getFacilitatorData();
      }
    },
    getEnterpriseData() {
      let { code, msg, data } = this.$api.sendSync(
        this.$mapapi.enterpriseUserList,
        this.form
      );
      if (code != 200 && code != 501) {
        this.$message.error(msg);
        this.tableData = [];
        return;
      }
      this.tableData = data.list;
    },
    getAgentData() {
      let { code, msg, data } = this.$api.sendSync(
        this.$mapapi.agentUserList,
        this.form
      );
      if (code != 200 && code != 501) {
        this.tableData = [];
        this.$message.error(msg);
        return;
      }
      this.tableData = data.list;
    },
    getFacilitatorData() {
      let { code, msg, data } = this.$api.sendSync(
        this.$mapapi.facilitatorUserList,
        this.form
      );
      if (code != 200 && code != 501) {
        this.tableData = [];
        this.$message.error(msg);
        return;
      }
      this.tableData = data.list;
      this.pagination = data.pagination;
    },
    reSet() {
      this.form.user_name = "";
      this.form.user_real_name = "";
      this.getData();
    }
  },
  created() {}
};
</script>


<style lang="less" :scope="true"  src="@/style/sManagement/user_managen.less" >
</style>